import React, { useState } from 'react';
import { Button, Text, TextInput, ToastAndroid, View } from 'react-native';
import AsyncStorage from '@react-native-async-storage/async-storage';

const Home = () => {
	const [user,setUser] = useState('');

	const storeData = async (key,value) => {
		try {
		   await AsyncStorage.setItem(key, value)
		} catch (e) {
			ToastAndroid.show(e,ToastAndroid.SHORT,ToastAndroid.TOP)
		}
	}
	const getData = async () => { 
		try {
		  const value = await AsyncStorage.getItem('user')
		  if(value !== null) {
			console.log(value)
		  }
		} catch(e) {
			ToastAndroid.show(e,ToastAndroid.SHORT,ToastAndroid.TOP)
		}
	}

	return (
		<View>
			<Text>首页</Text>
			<View>
				<View>
					<TextInput value={user} onChangeText={text => setUser(text)} />
				</View>
				<View>
					<Button title='存入user' onPress={() => storeData('user',user)} />
					<Button title='获取user' onPress={() => getData()} />
				</View>
			</View>
		</View>
	)
}

export default Home;